package vanScheduling;

import db.DriverDAO;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class Driver {

    private int id;
    private String name;
    private String email;
    private String phoneNumber;
    private ArrayList<TimeSlot> timeSlots;

    public Driver() {
    }

    public Driver(int id, String name, String email, String phoneNumber) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.phoneNumber = phoneNumber;
    }

    public Driver(int id, String name, String email, String phoneNumber, ArrayList<TimeSlot> timeSlots) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.phoneNumber = phoneNumber;
        this.timeSlots = timeSlots;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    private Driver editableDriver;
    
    public void setEditableDriver() {
        FacesContext fc = FacesContext.getCurrentInstance();
        Driver temp = (Driver)fc.getExternalContext().getRequestMap().get("driverL");
        editableDriver = temp;
    }

    public String addDriver() {
        if (DriverDAO.addDriver(this)) {
            return "driverList"; //TODO - Forward to time slots
        } else {
            return "driverAddFailure";
        }
    }

    public String deleteDriver() {
        if(DriverDAO.deleteDriver(this)) {
            return "driverList";
        } else {
            return "driverDeleteFailure";
        }
    }
    
    public List<Driver> getDrivers() {
        return DriverDAO.getDrivers();
    }

    public String editDriver() {
        setEditableDriver();
        return "editDriver";
    }
    
    @Override
    public String toString() {
        return this.name;
    }
}
